package org.xx.armory.web;

import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public final class MIMETypes {
    private static final String DEFAULT_MIME = "application/octet-stream";
    private static final Map<String, String> EXT_MIME_MAP = Collections.unmodifiableMap(createMimeTypesMap());

    private MIMETypes() {
        throw new UnsupportedOperationException();
    }

    private static Map<String, String> createMimeTypesMap() {
        final Map<String, String> ret = new HashMap<>();
        ret.put(".txt", "text/plain");
        ret.put(".text", "text/plain");
        ret.put(".xml", "application/xml");
        ret.put(".csv", "text/comma-separated-values");
        ret.put(".css", "text/css");
        ret.put(".js", "application/javascript");
        ret.put(".html", "text/html");
        ret.put(".htm", "text/html");
        ret.put(".bmp", "image/bmp");
        ret.put(".gif", "image/gif");
        ret.put(".png", "image/png");
        ret.put(".jpg", "image/jpeg");
        ret.put(".jpeg", "image/jpeg");
        ret.put(".ico", "image/x-icon");
        ret.put(".icon", "image/x-icon");
        ret.put(".swf", "application/x-shockwave-flash");
        ret.put(".pdf", "application/pdf");
        ret.put(".zip", "application/zip");
        ret.put(".bz", "application/x-bzip");
        ret.put(".bz2", "application/x-bzip2");
        ret.put(".tar", "application/x-tar");
        ret.put(".rar", "application/x-rar-compressed");
        ret.put(".xls", "application/vnd.ms-excel");
        ret.put(".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        ret.put(".doc", "application/msword");
        ret.put(".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        ret.put(".ppt", "application/vnd.ms-powerpoint");
        ret.put(".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
        return ret;
    }

    public static String getContentType(String ext) {
        ext = StringUtils.trimToEmpty(ext).toLowerCase();
        return EXT_MIME_MAP.getOrDefault(ext, DEFAULT_MIME);
    }
}
